<?php

declare(strict_types=1);

namespace app\admin\controller\auth;

use sheep\controller\Backend;
use app\admin\model\auth\Admin as AdminModel;
use sheep\exception\SheepException;
use think\facade\Db;

class Admin extends Backend
{
    protected $childAdminIds = [];

    public function initialize()
    {
        $this->model = new AdminModel;
        $this->childAdminIds = $this->auth()->getChildAdminIds(true);
    }

    /**
     * 管理员列表
     *
     * @return \think\Response
     */
    public function list()
    {
        $data = $this->model->sheepFilter()->whereIn('id', $this->childAdminIds)->with("role")->paginate(request()->param('list_rows', 10));

        return success('获取成功', $data);
    }


    /**
     * 管理员添加
     *
     * @return \think\Response
     */
    public function add()
    {

        $params = $this->request->only(['account', 'role_id', 'password', 'avatar', 'nickname', 'mobile', 'email', 'status']);
        $params = array_filter($params);
        $this->svalidate($params, ".add");

        $childRoleIds = $this->auth()->getChildRoleIds(false);
        if (!$this->auth()->isSuper() && !in_array($params['role_id'], $childRoleIds)) {
            throw new SheepException('请选择正确的角色组');
        }

        $admin = Db::transaction(function () use ($params) {
            return $this->model->save($params);
        });
        return success('保存成功', $admin);
    }

    /**
     * 管理员详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $admin = $this->auth()->user();
        if(!in_array($id, $this->childAdminIds) || $id == $admin->id) {
            throw new SheepException('无权限操作该管理员');
        }

        $data = $this->model->findOrFail($id);
        
        return success('获取成功', $data);
    }

    /**
     * 更新管理员(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $params = $this->request->only(['role_id', 'password', 'avatar', 'nickname', 'mobile', 'email', 'status']);

        $id = explode(',', $id);
        $admin = $this->auth()->user();

        foreach($id as $i) {
            $params['id'] = $i;
            $this->svalidate($params);
        }
        unset($params['id']);

        if (in_array($admin->id, $id) || array_diff($id, $this->childAdminIds)) {
            throw new SheepException("无权限操作该管理员");
        }
        if (isset($params['role_id'])) {
            $childRoleIds = $this->auth()->getChildRoleIds(false);
            if (!in_array($params['role_id'], $childRoleIds)) {
                throw new SheepException('禁止越权操作');
            }
        }
        
        Db::transaction(function () use ($id, $params) {
            foreach ($this->model->whereIn('id', $id)->cursor() as $admin) {
                $admin->save($params);
            }
        });

        return success('更新成功');
    }

    /**
     * 删除管理员(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id = explode(',', $id);

        $admin = $this->auth()->user();
        if (in_array($admin->id, $id) || array_diff($id, $this->childAdminIds)) {
            throw new SheepException("无权限操作该管理员");
        }

        Db::transaction(function () use ($id) {
            foreach ($this->model->whereIn('id', $id)->cursor() as $admin) {
                $admin->delete();
            }
        });

        return success('删除成功');
    }



    /**
     * 获取管理员的消息列表
     *
     * @return \think\Response
     */
    public function notifications()
    {
        $admin = $this->auth()->user();

        // 查询分页列表
        $notifications = $admin->notifications()->sheepFilter()->paginate(request()->param('list_rows', 10));

        return success('消息列表', $notifications);
    }


    /**
     * 指定消息标记已读
     *
     * @param string $id
     * @return void
     */
    public function notification($id)
    {
        $admin = $this->auth()->user();

        // 将要查询的列表全部标记为已读
        $notification = $admin->notifications()->findOrFail($id)->markAsRead();

        return success('已读成功', $notification);
    }


    /**
     * 删除已读消息
     *
     * @return void
     */
    public function delNotifications()
    {
        $admin = $this->auth()->user();

        // 将要查询的列表全部标记为已读
        $admin->notifications()->whereNotNull('read_time')->delete();

        return success('删除成功');
    }
}
